<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/17
 * Time: 11:51
 */

if (!(defined('IN_IA')))
{
    exit('Access Denied');
}

//服务类订单评价
class Comment_MxShopPage extends MobilePage
{

    public function main()
    {
        global $_W,$_GPC;

        if($_W['ispost'])
        {
            //验证令牌
            if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
            {
                show_json(0, '参数有误');
            }

            $this->submit();
            exit();
        }

        //获取令牌
        $token = $this->getToken();

        include $this->template();
    }

    protected function submit()
    {
        global $_GPC;

        $ordersn = intval($_GPC['ordersn']);
        $order = $this->checkorder($ordersn);

        $level = intval($_GPC['comment']['level']);
        $content = $_GPC['comment']['content'];
        $images = $_GPC['comment']['images'];

        if(empty($level) || empty($content))
        {
            show_json(0, '参数有误');
        }

        $nickname = pdo_fetchcolumn('SELECT nickname FROM ' . tablename('mx_shop_userinfo') . ' WHERE openid=:openid AND uniacid=:uniacid', array(':openid'=>$order['openid'], ':uniacid'=>$order['uniacid']));

        $data = array
        (
            'uniacid'=>$order['uniacid'],
            'orderid'=>$order['id'],
            'openid'=>$order['openid'],
            'mendianid'=>$order['mendian_id'],
            'goodsid'=>$order['goods_id'],
            'nickname'=>$nickname,
            'level'=>$level,
            'content'=>$content,
            'createtime'=>time(),
            'deleted'=>0,
            'checked'=>0,
        );

        if(!empty($images))
        {
            $data['images'] = serialize($images);
        }
        pdo_insert('mx_shop_service_order_comment', $data);

        pdo_update('mx_shop_service_order', array('iscomment'=>1, 'commenttime'=>time()), array('id'=>$order['id']));

        show_json(1, '评价成功');
    }

    protected function checkorder($ordersn)
    {
        global $_W;

        $uniacid = $_W['uniacid'];
        $openid = $_W['openid'];

        if(empty($ordersn))
        {
            show_json(0, '参数有误');
        }
        $order = pdo_fetch('SELECT * FROM ' . tablename('mx_shop_service_order') . ' WHERE ordersn=:ordersn AND openid=:openid AND deleted=0 AND userdeleted=0', array(':id'=>$ordersn, ':openid'=>$openid));
        if(empty($order))
        {
            show_json(0, '该订单不存在');
        }

        if($order['status'] != 3)
        {
            show_json(0, '订单未完成，不能评价！');
        }

        if($order['iscomment'] > 0)
        {
            show_json(0, '该订单已评价过了');
        }

        return $order;
    }

    public function comdel()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token'])){
            show_json(0, '参数有误');
        }

        $openid = $_W['openid'];

        $ordersn = intval($_GPC['ordersn']);
        $order = $this->checkorder($ordersn);

        $comment = pdo_fetch('SELECT * FROM ' . tablename('mx_shop_service_order_comment') . ' WHERE orderid=:orderid AND openid=:openid AND deleted=0', array(':orderid'=>$order['id'], ':openid'=>$openid));
        if(empty($comment))
        {
            show_json(0, '评价不存在！');
        }

        pdo_update('mx_shop_service_order', array('iscomment'=>0), array('id'=>$comment['orderid']));
        pdo_update('mx_shop_service_order_comment', array('deleted'=>1, 'deletetime'=>time()), array('id'=>$comment['id']));

        show_json(1, '评价已删除！');
    }


}